package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcRoomSortMapper;
import com.csun.cmny.provider.model.domain.OmcRoomSort;
import com.csun.cmny.provider.model.dto.RoomSortQueryDto;
import com.csun.cmny.provider.model.dto.RoomSortSaveDto;
import com.csun.cmny.provider.model.vo.RoomSortVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcRoomSortService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-03-11 13:45
 * @Description: the class omc room sort service impl
 */
@Slf4j
@Service
public class OmcRoomSortServiceImpl extends BaseService<OmcRoomSort> implements OmcRoomSortService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcRoomSortMapper omcRoomSortMapper;

    /**
     * query room sort
     *
     * @param roomSortQueryDto  room sort query dto
     *
     * @return room sort vo
     */
    @Override
    public RoomSortVo queryRoomSort(RoomSortQueryDto roomSortQueryDto) {

        OmcRoomSort omcRoomSortQuery = new OmcRoomSort();
        BeanUtils.copyProperties(roomSortQueryDto, omcRoomSortQuery);
        omcRoomSortQuery = omcRoomSortMapper.selectOne(omcRoomSortQuery);
        if (PublicUtil.isNotEmpty(omcRoomSortQuery)) {
            RoomSortVo roomSortVo = new RoomSortVo();
            BeanUtils.copyProperties(omcRoomSortQuery, roomSortVo);

            return roomSortVo;
        }

        return null;
    }

    /**
     * save room sort
     *
     * @param roomSortSaveDto room sort save dto
     * @param loginAuthDto    login auth dto
     * @return the int.
     */
    @Override
    public Integer saveRoomSort(RoomSortSaveDto roomSortSaveDto, LoginAuthDto loginAuthDto) {

        Integer result = 0;

        OmcRoomSort omcRoomSortQuery = new OmcRoomSort();
        omcRoomSortQuery.setBuildingId(roomSortSaveDto.getBuildingId());
        omcRoomSortQuery.setFloorNo(roomSortSaveDto.getFloorNo());
        omcRoomSortQuery = omcRoomSortMapper.selectOne(omcRoomSortQuery);
        if (PublicUtil.isNotEmpty(omcRoomSortQuery)) {
            BeanUtils.copyProperties(roomSortSaveDto, omcRoomSortQuery);
            omcRoomSortQuery.setUpdateInfo(loginAuthDto);

            result = omcRoomSortMapper.updateByPrimaryKeySelective(omcRoomSortQuery);
        } else {
            omcRoomSortQuery = new OmcRoomSort();
            omcRoomSortQuery.setUpdateInfo(loginAuthDto);
            BeanUtils.copyProperties(roomSortSaveDto, omcRoomSortQuery);

            result = omcRoomSortMapper.insertSelective(omcRoomSortQuery);
        }

        return result;
    }
}
